/*
 * RightsAgent.java
 *
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.mikrotubula.agents;

import info.mikrotubula.db.Groups;
import info.mikrotubula.db.Profiles;
import info.mikrotubula.db.Rights;
import info.mikrotubula.managers.ProfilesManagerLocal;
import info.mikrotubula.managers.RightsManagerLocal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;


/**
 *
 * @author Krystian
 */
public class RightsAgent {

    @EJB
    private ProfilesManagerLocal profilesManagerBean;
    
    @EJB
    private RightsManagerLocal rightManagerBean;
    private List<Groups> groups;
    private List<Rights> rights;
    private List<Profiles> profiles;
    private String profileName;
    private ArrayList<SelectItem> profilesList; 
    private Integer selectedProfileId;
    private Integer selectedRightIndex;
    private Integer selectedGroupIndex;
    
    private Boolean showNewProfile = false;
    
    /** Creates a new instance of RightsAgent */
    public RightsAgent() {
    }
    
    public void createProfile() {
        Profiles profile = new Profiles();
        profile.setPrfName(this.profileName);
        if (profilesManagerBean.checkIfExists(profile)) {
            FacesContext.getCurrentInstance().addMessage("login-box", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Profile: Profil o takiej nazwie ju� istnieje", "Porfil o podanej nazwie ju� istnieje"));
            return;
        }
        profilesManagerBean.create(profile);
        if (profile.getPrfId() == null) {
            FacesContext.getCurrentInstance().addMessage("login-box", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Profile: Wystąpił błąd podczas tworzenia profilu", "Wystąpił błąd podczas tworzenia profilu"));
            return;
        }
        profiles = null;
        getProfiles();
        profilesList = null;
        getProfilesList();
        this.showNewProfile = false;
        this.profileName = null;
        FacesContext.getCurrentInstance().addMessage("login-box", new FacesMessage(FacesMessage.SEVERITY_INFO,"Profile: Pomyślnie dodano profil", "Pomyślnie dodano profil"));
    }
    
    public void refresh() {
        this.rights = null;
        this.groups = null;
        this.profiles = null;
        getRights();
        getGroups();
        getProfiles();
        setSelectedProfileId(-1);
    }
 
    public List<Groups> getGroups() {
        if (groups == null) this.groups = rightManagerBean.getGroupsList();
        return groups;
    }
    
    public void save() {
        Profiles profile = new Profiles();
        profile.setPrfId(this.selectedProfileId);
        getRightsFromGroups(this.groups);
        profilesManagerBean.saveProfileRights(profile, this.getRights());
    }
    
    public void showNewProfilePanel() {
        this.showNewProfile = true;
    }
    
    public void hideNewProfilePanel() {
        this.showNewProfile = false;
    }
    
    public void selectProfile() {
        //podmiana ustawień uprawnień
        if (this.selectedProfileId != null && this.selectedProfileId > 0) {
            Profiles profile = new Profiles();
            profile.setPrfId(selectedProfileId);
            this.groups = getGroupsForProfile(this.groups, profilesManagerBean.findById(profile));
        }
     }

    public void setGroups(List<Groups> groups) {
        this.groups = groups;
    }
    
    public List<Rights> getRights() {
        if(rights==null) this.rights = rightManagerBean.getRightsList();
        return rights;
    }

    public void setRights(List<Rights> rights) {
        this.rights = rights;
    }

    public List<Profiles> getProfiles() {
        if(profiles==null) this.profiles = profilesManagerBean.list();
        return profiles;
    }

    public void setProfiles(List<Profiles> profiles) {
        this.profiles = profiles;
    }

    public String getProfileName() {
        return profileName;
    }
    
    public void setProfileName(String profileName) {
        this.profileName = profileName;
    }

    public ArrayList getProfilesList() {
        if (profilesList == null) {
            profilesList = new ArrayList<SelectItem>();
            profilesList.add(new SelectItem("-1", "Wybierz profil..."));
            for (Profiles profile : this.getProfiles())
                profilesList.add(new SelectItem(profile.getPrfId().toString(), profile.getPrfName()));
        }
        return profilesList;
    }

    public void setProfilesList(ArrayList<SelectItem> profilesList) {
        this.profilesList = profilesList;
    }

    public Integer getSelectedProfileId() {
        return selectedProfileId;
    }

    public void setSelectedProfileId(Integer selectedProfileId) {
        this.selectedProfileId = selectedProfileId;
    }

    private List<Groups> getGroupsForProfile(List<Groups> groups, Profiles profile) {
          for (Groups group : groups)
                for (Rights right : group.getRights())
                    if (profile.getRights() != null) 
                        right.setSelected(profile.getRights().contains(right)); 
                    else 
                        right.setSelected((boolean)false);
          return groups;
    }

    public Boolean getShowNewProfile() {
        return showNewProfile;
    }

    public void setShowNewProfile(Boolean showNewProfile) {
        this.showNewProfile = showNewProfile;
    }

    public Integer getSelectedRightIndex() {
        return selectedRightIndex;
    }

    public void setSelectedRightIndex(Integer selectedRightIndex) {
        this.selectedRightIndex = selectedRightIndex;
    }

    public Integer getSelectedGroupIndex() {
        return selectedGroupIndex;
    }

    public void setSelectedGroupIndex(Integer selectedGroupIndex) {
        this.selectedGroupIndex = selectedGroupIndex;
    }

    private void getRightsFromGroups(List<Groups> groups) {
        this.getRights().clear();
        for (Groups group : groups)
            for (Rights rightFromGroup : group.getRights())
                this.rights.add(rightFromGroup);
    }
}
